package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.Result;
import com.orientation.dto.BatchVo;
import com.orientation.entity.Reserve;
import com.orientation.service.ReserveService;
import com.orientation.vo.ReserveVo;
import com.orientation.vo.StudentProgressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 报到 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */
@Api(tags = "预约报道信息")
@RestController
@RequestMapping("/reserve")
public class ReserveController {

    @Resource
    private ReserveService reserveService;

    @ApiOperation("添加")
    @PostMapping
    public boolean save(@RequestBody Reserve reserve) {
        reserve.setStatus(1);
        return reserveService.save(reserve);
    }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody Reserve reserve) {
        return reserveService.updateById(reserve);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return reserveService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Reserve findById(@PathVariable String id) {
        return reserveService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<Reserve> findAll() {
        return reserveService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public IPage<Reserve> findPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return reserveService.page(new Page<>(pageNum, pageSize));
    }

    @ApiOperation("统计报道学生人数完成进度")
    @GetMapping("/progress")
    public ResponseEntity<Result> queryProgress(@RequestParam(value = "collegeId", required = false) String collegeId) {
        StudentProgressVo progress = reserveService.progress(collegeId);
        return ResponseEntity.ok(new Result(progress));
    }

    @ApiOperation("批量审核学生报道预约")
    @PostMapping("/auditBatch")
    public ResponseEntity<Result> auditBatch(@RequestBody BatchVo batchVo) {
        reserveService.auditBatch(batchVo);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("分页查询学生列表")
    @GetMapping("/queryReserveVo")
    public ResponseEntity<Result> queryReserveVo(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                 @RequestParam(value = "studentName", required = false) String studentName,
                                                 @RequestParam(value = "majorId", required = false) String majorId,
                                                 @RequestParam(value = "status", required = false) String status,
                                                 @RequestParam(value = "studentId", required = false) String studentId,
                                                 @RequestParam(value = "classId", required = false) String classId,
                                                 @RequestParam(value = "startTime", required = false) String startTime,
                                                 @RequestParam(value = "endTime", required = false) String  endTime,
                                                 @RequestParam(defaultValue = "") String adviserId) {
        Page<ReserveVo> reserveVoPage = reserveService.queryReserveVo(pageNum, pageSize, studentName, majorId, status, studentId, classId, startTime, endTime,adviserId);
        return ResponseEntity.ok(Result.success(reserveVoPage));
    }

    @ApiOperation("审核学生报道预约")
    @GetMapping("/audit/{id}")
    public boolean audit(@PathVariable String id) {
        //审核
        Reserve reserve = reserveService.getById(id);
        if (reserve.getStatus() == 1) {
            reserve.setStatus(2);
            return reserveService.updateById(reserve);
        } else {
            return false;
        }
    }


//    @ApiOperation("批量审核学生报道预约")
//    @PostMapping("/auditBatch")
//    public void auditBatch(@RequestBody BatchVo batchVo) {
//        List<Reserve> reserveList = reserveService.listByIds(batchVo.getIds());
//        for (Reserve reserve : reserveList) {
//            reserve.setStatus(2);
//        }
//        reserveService.updateBatchById(reserveList);
//    }

    @ApiOperation("学生报道")
    @GetMapping("/reserve/{id}")
    public boolean reserve(@PathVariable String id) {
        //报道
        Reserve reserve = reserveService.getById(id);
        if (reserve.getStatus() == 2) {
            reserve.setStatus(3);
            return reserveService.updateById(reserve);
        } else {
            return false;
        }
    }


    @ApiOperation("报道预约分页查询学生列表全部")
    @GetMapping("/queryReserveVoAll")
    public ResponseEntity<Result> queryReserveAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                 @RequestParam(value = "id", required = false) String id,
                                                 @RequestParam(value = "status", required = false) String status,
                                                 @RequestParam(value = "studentId", required = false) String studentId,
                                                 @RequestParam(value = "realTime", required = false) String realTime) {
        IPage<ReserveVo> reserveVoPage = reserveService.queryReserveVoAll(pageNum, pageSize, id, status, studentId, realTime);
        return ResponseEntity.ok(Result.success(reserveVoPage));
    }
}

